package com.ljkj.blockpay.basedata;

import android.content.Context;



import androidx.annotation.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by user on 2016/11/4.
 */

public abstract class BasePresenterImpl<V extends BaseView> {
    private V view;
    private Context mContext;
//    private LoadingDialog loadingDialog;
//    private LoadingDialog2 loadingDialog2;


    public CompositeDisposable mDisposable;

    public BasePresenterImpl(@NonNull Context context, @NonNull V view) {
        this.view = view;
        this.mContext = context;
        mDisposable = new CompositeDisposable();
    }

    protected V getView() {
        return view;
    }


    public void showLoading() {
//        if (loadingDialog == null) {
//            loadingDialog = new LoadingDialog.Builder(mContext).create();
//        }
//        loadingDialog.show();
    }

    public void dismisLoading() {
//        if (loadingDialog != null) {
//            loadingDialog.dismiss();
//        }
    }

    public void showLoading2(String hint) {
//        if (loadingDialog2 == null) {
//            loadingDialog2 = new LoadingDialog2.Builder(mContext, hint).create();
//        }
//        loadingDialog2.show();
    }


    public void dismisLoading2() {
//        if (loadingDialog2 != null) {
//            loadingDialog2.dismiss();
//        }
    }

    protected Context getContext() {
        return mContext;
    }

    protected boolean isDetachView() {
        return true;
    }

    public void detachView() {
        if (isDetachView()) {
//            this.view = null;
            onUnSubscribe();
        }
    }

    //RxJava2订阅
    public void addDisposable(Disposable disposable) {
        if (mDisposable != null) {
            mDisposable.add(disposable);
        }
    }

    //RxJava2取消注册，以避免内存泄露
    private void onUnSubscribe() {
        if (mDisposable != null && mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
        mDisposable = null;
        mContext = null;
    }
}
